/**
 * @author Vitor Franco do Carmo (vitor.carmo@cpmbraxis.com)
 * @since 03/08/2013
 *
 */
package br.com.vfcfw.test.functional.browser;

import java.io.File;

import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

/**
 * Classe responsável por rodar o Chrome nos testes funcionais.
 * 
 * @author Vitor Franco do Carmo (vitor.carmo@cpmbraxis.com)
 * @since 03/08/2013
 * 
 */
@RunWith(BlockJUnit4ClassRunner.class)
public class ChromeNavegador implements INavegador {

    private static ChromeDriverService service;

    private static WebDriver driver;

    /**
     * {@inheritDoc}
     * 
     * @see br.com.vfcfw.test.functional.browser.INavegador#inicializarServico()
     */
    @Override
    public void inicializarServico() {

        service = new ChromeDriverService.Builder()
            .usingChromeDriverExecutable(new File("/Users/vitor/Dev/Java/Libs/chromedriver")).usingAnyFreePort()
            .build();

        try {

            service.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see br.com.vfcfw.test.functional.browser.INavegador#finalizarServico()
     */
    @Override
    public void finalizarServico() {

        service.stop();
    }

    /**
     * {@inheritDoc}
     * 
     * @see br.com.vfcfw.test.functional.browser.INavegador#inicializarTeste()
     */
    @Override
    public void inicializarTeste() {

        driver = new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
    }

    /**
     * {@inheritDoc}
     * 
     * @see br.com.vfcfw.test.functional.browser.INavegador#finalizarTeste()
     */
    @Override
    public void finalizarTeste() {

        if (driver != null) {

            driver.close();
        }
    }

    /**
     * {@inheritDoc}
     * 
     * @see br.com.vfcfw.test.functional.browser.INavegador#obterDriver()
     */
    @Override
    public WebDriver obterDriver() {

        return driver;
    }

}
